<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">

          <a-col :md="6" :sm="8">
            <a-form-item label="职务编码">
              <j-input placeholder="请输入职务编码" v-model="queryParam.code"></j-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="职务名称">
              <j-input placeholder="请输入职务名称" v-model="queryParam.name"></j-input>
            </a-form-item>
          </a-col>
          <!--<template v-if="toggleSearchStatus">-->
          <!--<a-col :md="6" :sm="8">-->
          <!--<a-form-item label="职级">-->
          <!--<j-dict-select-tag v-model="queryParam.postRank" placeholder="请选择职级" dictCode="position_rank"/>-->
          <!--</a-form-item>-->
          <!--</a-col>-->

          <!--</template>-->
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <!--<a @click="handleToggleSearch" style="margin-left: 8px">-->
              <!--{{ toggleSearchStatus ? '收起' : '展开' }}-->
              <!--<a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>-->
              <!--</a>-->
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-has="'position:add'" @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <!--<a-button type="primary" icon="download" @click="handleExportXls('职务表')">导出</a-button>-->
      <!--<a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">-->
      <!--<a-button type="primary" icon="import">导入</a-button>-->
      <!--</a-upload>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="doBatchDel">
            <a-icon type="delete"/>
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
          selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <span slot="status" slot-scope="text, record">
          <a-switch
            checked-children="开启"
            un-checked-children="禁用"
            :checked="record.status === 1 ? true:false"
            @click="statusEdit(record)"/>
        </span>

        <span slot="action" slot-scope="text, record">
          <a v-has="'position:edit'" @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical"/>
          <a-popconfirm v-has="'position:delete'" title="确定删除吗?" @confirm="() => doDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>

        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <sysPosition-modal ref="modalForm" @ok="modalFormOk"></sysPosition-modal>
  </a-card>
</template>

<script>
  import SysPositionModal from './modules/SysPositionModal'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import JDictSelectTag from '@comp/dict/JDictSelectTag'
  import { api, editPosition } from './service.js'

  export default {
    name: 'SysPositionList',
    mixins: [JeecgListMixin],
    components: {
      SysPositionModal,
      JDictSelectTag
    },
    data() {
      return {
        description: '职务表管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function(t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '职务编码',
            dataIndex: 'code'
          },
          {
            title: '职务名称',
            dataIndex: 'name'
          },
          {
            title: '状态',
            dataIndex: 'status',
            align: 'center',
            scopedSlots: { customRender: 'status' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: api.list,
          delete: '',
          deleteBatch: ''
        }
      }
    },
    computed: {
      // importExcelUrl: function() {
      //   return apiUrl.importExcelUrl(this.url.importExcelUrl);
      // }
    },
    methods: {
      statusEdit(formData) {
        const that = this
        if (formData.status === 1) {
          formData.status = 0
        } else {
          formData.status = 1
        }
        editPosition(formData).then((res) => {
          if (res.success) {
            that.$message.success('操作成功')
            that.$emit('ok')
          } else {
            that.$message.warning('操作失败')
          }
        })
      },
      doDelete(ids) {
        this.url.delete = api.delete(ids);
        this.handleDelete(ids);
      },
      doBatchDel() {
        let ids = '';
        for (let a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ',';
        }
        this.url.deleteBatch = api.delete(ids);
        this.batchDel();
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
